<?php

	/**
	 * File : list_dir.php
	 * Description : Affiche un répertoire sous format XML
	 * Date : 07.08.2012 12:59:04
	 * 
	 * This file is part of Phenix.  Phenix is free software: you can
	 * redistribute it and/or modify it under the terms of the GNU General Public
	 * License as published by the Free Software Foundation, version 2.
	 *
	 * This program is distributed in the hope that it will be useful, but WITHOUT
	 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
	 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
	 * details.
	 *
	 * You should have received a copy of the GNU General Public License along with
	 * this program; if not, write to the Free Software Foundation, Inc., 51
	 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
	 *
	 * Nils Schaetti <n.schaetti@gmail.com>
	**/

	// En tête XML
	echo "<?xml version=\"1.0\"?>";

	// Includes
	require_once('core/libphenix.php');
	require_once('KTK/ktk.php');

	// Buffering
	ob_start();
	
	// Document type XML
	header("content-type: application/xml");

	// Démarre la session
	session_start();
	
	// Connexion ssh
	if(!($ssh_handler = lp_ssh_connect('localhost',$_SESSION['username'],$_SESSION['password'])))
	{
		exit;
	}
	
	// Répertoire à lister
	if(!isset($_GET['dir']))
		$dir = '.';
	else
		$dir = $_GET['dir'];
	
	// Fichiers cachés ou non?
	if(!isset($_GET['showHiddenFiles']))
		$showHiddenFiles = false;
	else
		$showHiddenFiles = $_GET['showHiddenFiles'];
		
	// Ordre
	if(!isset($_GET['order']))
		$order = 0;
	else
		$order = $_GET['order'];
		
	// Selon l'order
	switch($order)
	{
		case 0:
			$args = '';
			break;
		case 1:
			$args = 'S';
			break;
		case 2:
			$args = 't';
			break;
	}
	
	// Tag fichier
	echo "<files>";
		
	// Execute une commande
	if(($files = lp_ls($ssh_handler,$dir,$showHiddenFiles,$args)) === false)
	{
		echo "<error>You can't list this directory</error>";
	}
	else
	{
		// Pour chaque fichier
		if(count($files) > 0 && $files !== '')
		{
			foreach($files as $file)
			{
				// Tag File
				echo "<file>";
				
				// Type de fichier
				if($file->is_dir())
					echo "<type>dir</type>";
				else
					echo "<type>file</type>";

				// Nom de fichier
				echo "<name>".$file->getFilename()."</name>";

				// Droits
				echo "<permissions>".$file->getRights()."</permissions>";
			
				// Propriétaire
				echo "<owner>".$file->getOwner()."</owner>";
				
				// Taille
				echo "<size>".$file->getSize()."</size>";
			
				// Dernière modification
				echo "<last>".$file->getLastModification()."</last>";
				
				// Fin File
				echo "</file>";
			}
		}
	}
	
	// Ferme le tag fichier
	echo '</files>';
		
	// Ferme la connexion ssh
	lp_ssh_close($ssh_handler);	

	// Fin buffering
	ob_end_flush();

?>
